﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using InstantNaukriDAL;
using System.Collections.Generic;

public partial class PostJobs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string roleid = "";
            clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
            if (Request.QueryString["roleid"] != null)
                roleid = clsEncryptionDecryption.Decrypt(Request.QueryString["roleid"].ToString());
            if (roleid == "3")
            {
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                if (Session["UserId"] != null && Session["EmployerId"] != null)
                {
                    dvPostResume.Visible = true;
                    clsJobPostDAL objclsJobPostDAL = new clsJobPostDAL();
                    List<IN_TB_JOB_POST> objIN_TB_JOB_POSTList = new List<IN_TB_JOB_POST>();
                    objIN_TB_JOB_POSTList = objclsJobPostDAL.GetJobPostByEmployerId(Convert.ToInt32(Session["EmployerId"].ToString()));
                    //grdJobsPosted.DataSource = objIN_TB_JOB_POSTList;
                    //grdJobsPosted.DataBind();
                    rptrJobPosted.DataSource = objIN_TB_JOB_POSTList;
                    rptrJobPosted.DataBind();
                    Fillddls();
                }
            }
        }
    }

    protected void Fillddls()
    {
        try
        {
            // Project Location
            clsCity objclsCity = new clsCity();
            List<IN_TB_CITY> objIN_TB_CITY = new List<IN_TB_CITY>();
            objIN_TB_CITY = objclsCity.GetAllCities();
            ddlJobLocation.Items.Clear();
            ddlJobLocation.AppendDataBoundItems = true;
            ddlJobLocation.DataTextField = "CITY";
            ddlJobLocation.DataValueField = "ID";
            ddlJobLocation.Items.Add("Select");
            ddlJobLocation.Items[0].Value = "0";
            ddlJobLocation.DataSource = objIN_TB_CITY;
            ddlJobLocation.DataBind();

            // Functional Area
            clsFunctionalAreaDAL objclsFunctionalAreaDAL = new clsFunctionalAreaDAL();
            List<IN_TB_FUNCTIONAL_AREA> objIN_TB_FUNCTIONAL_AREA = new List<IN_TB_FUNCTIONAL_AREA>();
            objIN_TB_FUNCTIONAL_AREA = objclsFunctionalAreaDAL.GetAllFunctionalAreas();
            ddlFunctionalArea.Items.Clear();
            ddlFunctionalArea.AppendDataBoundItems = true;
            ddlFunctionalArea.DataTextField = "FUNCTIONAL_AREA";
            ddlFunctionalArea.DataValueField = "ID";
            ddlFunctionalArea.Items.Add("Select");
            ddlFunctionalArea.Items[0].Value = "0";
            ddlFunctionalArea.DataSource = objIN_TB_FUNCTIONAL_AREA;
            ddlFunctionalArea.DataBind();

            //Min Experience Required
            ddlMinExpRequired.Items.Clear();
            ddlMinExpRequired.AppendDataBoundItems = true;
            ddlMinExpRequired.Items.Add("Select");
            ddlMinExpRequired.Items[0].Value = "0";
            for (int i = 1; i <= 90; i++)
            {
                ddlMinExpRequired.Items.Add(i.ToString());
                ddlMinExpRequired.Items[i].Value = i.ToString();
            }
            ddlMinExpRequired.DataBind();

            // Max Experience Required
            ddlMaxExpRequired.Items.Clear();
            ddlMaxExpRequired.AppendDataBoundItems = true;
            ddlMaxExpRequired.Items.Add("Select");
            ddlMaxExpRequired.Items[0].Value = "0";
            for (int i = 1; i <= 90; i++)
            {
                ddlMaxExpRequired.Items.Add(i.ToString());
                ddlMaxExpRequired.Items[i].Value = i.ToString();
            }
            ddlMaxExpRequired.DataBind();
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void btnPostJob_Click(object sender, EventArgs e)
    {
        try
        {
            if (Session["EmployerId"] != null)
            {
                IN_TB_JOB_POST objIN_TB_JOB_POSTD = new IN_TB_JOB_POST();
                objIN_TB_JOB_POSTD.JOB_TITLE = txtJobTitle.Text.Trim();
                objIN_TB_JOB_POSTD.SKILLS_REQUIRED = txtKeySkills.Text;
                objIN_TB_JOB_POSTD.MIN_EXPERIENCE_REQUIRED = Convert.ToInt32(ddlMinExpRequired.SelectedValue);
                objIN_TB_JOB_POSTD.MAX_EXPERIENCE_REQUIRED = Convert.ToInt32(ddlMaxExpRequired.SelectedValue);
                objIN_TB_JOB_POSTD.JOB_LOCATION = Convert.ToInt32(ddlJobLocation.SelectedValue);
                objIN_TB_JOB_POSTD.JOB_DESCRIPTION = txtJobDescription.Text;
                objIN_TB_JOB_POSTD.QUALIFICATION_REQUIRED = txtQualificationRequired.Text;
                objIN_TB_JOB_POSTD.SALARY_OFFERED = txtSalaryOffered.Text;
                objIN_TB_JOB_POSTD.FUNCTIONAL_AREA = Convert.ToInt32(ddlFunctionalArea.SelectedValue);
                objIN_TB_JOB_POSTD.EMPLOYER_ID = Convert.ToInt32(Session["EmployerId"].ToString());
                objIN_TB_JOB_POSTD.STATUS = 1;
                objIN_TB_JOB_POSTD.CREATED_BY = Convert.ToInt32(Session["UserId"].ToString());
                objIN_TB_JOB_POSTD.CREATED_ON = DateTime.Now;
                objIN_TB_JOB_POSTD.MODIFIED_BY = Convert.ToInt32(Session["UserId"].ToString());
                objIN_TB_JOB_POSTD.MODIFIED_ON = DateTime.Now;
                clsJobPostDAL objclsJobPostDAL = new clsJobPostDAL();
                objclsJobPostDAL.SaveJobPost(objIN_TB_JOB_POSTD);
                objclsJobPostDAL = new clsJobPostDAL();
                List<IN_TB_JOB_POST> objIN_TB_JOB_POSTList = new List<IN_TB_JOB_POST>();
                objIN_TB_JOB_POSTList = objclsJobPostDAL.GetJobPostByEmployerId(Convert.ToInt32(Session["EmployerId"].ToString()));
                //grdJobsPosted.DataSource = objIN_TB_JOB_POSTList;
                //grdJobsPosted.DataBind();
                rptrJobPosted.DataSource = objIN_TB_JOB_POSTList;
                rptrJobPosted.DataBind();

                ClearFields();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void ClearFields()
    {
        try
        {
            txtJobTitle.Text = string.Empty;
            txtKeySkills.Text = string.Empty;
            ddlMinExpRequired.SelectedIndex = 0;
            ddlMaxExpRequired.SelectedIndex = 0;
            ddlJobLocation.SelectedIndex = 0;
            txtJobDescription.Text = string.Empty;
            txtQualificationRequired.Text = string.Empty;
            txtSalaryOffered.Text = string.Empty;
            ddlFunctionalArea.SelectedIndex = 0;
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void grdJobsPosted_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Getting Job Location
                int Job_locationId = Convert.ToInt32(e.Row.Cells[4].Text);
                clsCity objclsCity = new clsCity();
                IN_TB_CITY objIN_TB_CITY = new IN_TB_CITY();
                objIN_TB_CITY = objclsCity.GetCityById(Job_locationId);
                e.Row.Cells[4].Text = objIN_TB_CITY.CITY;

                // Geting Functional Area
                int Functional_AreaId = Convert.ToInt32(e.Row.Cells[8].Text);
                clsFunctionalAreaDAL objclsFunctionalAreaDAL = new clsFunctionalAreaDAL();
                IN_TB_FUNCTIONAL_AREA objIN_TB_FUNCTIONAL_AREA = new IN_TB_FUNCTIONAL_AREA();
                objIN_TB_FUNCTIONAL_AREA = objclsFunctionalAreaDAL.GetAllFunctionalAreaById(Functional_AreaId);
                e.Row.Cells[8].Text = objIN_TB_FUNCTIONAL_AREA.FUNCTIONAL_AREA;

            }
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void rptrJobPosted_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                IN_TB_JOB_POST objIN_TB_JOB_POST = new IN_TB_JOB_POST();
                objIN_TB_JOB_POST = (IN_TB_JOB_POST)e.Item.DataItem;
                //Getting Job Location
                int Job_locationId = Convert.ToInt32(objIN_TB_JOB_POST.JOB_LOCATION);
                clsCity objclsCity = new clsCity();
                IN_TB_CITY objIN_TB_CITY = new IN_TB_CITY();
                objIN_TB_CITY = objclsCity.GetCityById(Job_locationId);
                if (objIN_TB_CITY != null)
                {
                    var lblJobLocation = (Label)e.Item.FindControl("lblJobLocation");
                    lblJobLocation.Text = objIN_TB_CITY.CITY;
                }
                // Geting Functional Area
                int Functional_AreaId = Convert.ToInt32(objIN_TB_JOB_POST.FUNCTIONAL_AREA);
                clsFunctionalAreaDAL objclsFunctionalAreaDAL = new clsFunctionalAreaDAL();
                IN_TB_FUNCTIONAL_AREA objIN_TB_FUNCTIONAL_AREA = new IN_TB_FUNCTIONAL_AREA();
                objIN_TB_FUNCTIONAL_AREA = objclsFunctionalAreaDAL.GetAllFunctionalAreaById(Functional_AreaId);
                if (objIN_TB_FUNCTIONAL_AREA != null)
                {
                    var lblFunctionalArea = (Label)e.Item.FindControl("lblFunctionalArea");
                    lblFunctionalArea.Text = objIN_TB_FUNCTIONAL_AREA.FUNCTIONAL_AREA;
                }
                DateTime dt = Convert.ToDateTime(objIN_TB_JOB_POST.CREATED_ON);
                var lblCreatedOn = (Label)e.Item.FindControl("lblCreatedOn");
                lblCreatedOn.Text = dt.ToString("dd-MMM-yyyy");
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}
